package com.chwn.curiousdiary.utils;

import com.chwn.curiousdiary.api.IUserApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.RegisterBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by Myshikin on 2017/6/17.
 */

public class SendCodeUtil {

    public interface CallBack {
        void onStart();

        /**
         * @param info
         *         该手机号是否注册过
         */
        void onSuccess(RegisterBean info);

        /**
         * @param text
         * @param backType
         *         0--失败  1--成功  2--跳转登录
         */
        void onFail(String text, int backType);
    }

    public static void sendVerifyCode(String phone, final int type, String unionid, final CallBack callBack) {
        NetWork.getInstance()
                .getApiService(IUserApi.class)
                .sendSms(phone, Constant.OSTYPE, type, unionid)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        callBack.onStart();
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<RegisterBean>>>() {
                    @Override
                    public void onCompleted() {
                        DialogUtil.dismissWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFail("发送验证码失败", 0);
                    }

                    @Override
                    public void onNext(HttpResult<List<RegisterBean>> userHttpResult) {
                        if (userHttpResult.getResult() == Constant.RESULT_ERROR) {
                            switch (type) {
                                case Constant.FGT_PWD:
                                case Constant.BIND_PHONE:
                                    callBack.onFail(userHttpResult.getErrorMessage(), 0);
                                    break;
                                case Constant.REGISTER:

                                    if ("该手机已被注册，请更换手机号".equals(userHttpResult.getErrorMessage())) {
                                        callBack.onFail("此账号已注册 请直接登录", 2);
                                    } else {
                                        callBack.onFail(userHttpResult.getErrorMessage(), 0);
                                    }
                                    break;
                            }

                        } else {
//                            Logger.e(userHttpResult.getResdata().get(0).getNumber());
                            callBack.onSuccess(userHttpResult.getResdata().get(0));
                        }
                    }
                });
    }
}
